package com.ljfiction.book.activitys;

import android.app.Activity;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.ljfiction.book.R;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.SharedPreferencesUtil;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cookie.store.PersistentCookieStore;
import com.lzy.okhttputils.model.HttpHeaders;
import com.mob.MobSDK;
import com.scwang.smartrefresh.header.DeliveryHeader;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;


import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

import com.umeng.commonsdk.UMConfigure;

import org.xutils.BuildConfig;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Headers;


/**
 * Created by Administrator on 2017/7/4.
 */

public class MyApication extends Application {
   // public static RequestQueue volleyQueue;
    private static Context context;
    private List<Activity> oList;//用于存放所有启动的Activity的集合
    // public static RefWatcher mRefWatcher;

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        /*
         * 热修复
         * */
        SophixManager.getInstance().queryAndLoadNewPatch();
        //渠道统计
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);

       /* if (isMainProcess()) {
            OpenInstall.init(this);
        }*/
        oList = new ArrayList<Activity>();
        refresh();
        //注册第三方库xutil
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
        // sInstance = this;

        MobSDK.init(this);
        context = getApplicationContext();
        initPrefs();
        //volleyQueue = Volley.newRequestQueue(context);
OkHttpUtils.init(this);



       /* //检测app
        if (LeakCanary.isInAnalyzerProcess(this)) {//1
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        mRefWatcher = LeakCanary.install(this);*/

    }

    /* public boolean isMainProcess() {
         int pid = android.os.Process.myPid();
         ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
         for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
             if (appProcess.pid == pid) {
                 return getApplicationInfo().packageName.equals(appProcess.processName);
             }
         }
         return false;
     }*/
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
        initHotfix();

    }

    private void initHotfix() {
        String appVersion = "2.5";
        try {
            appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (Exception e) {
            appVersion = "2.5";

        }
        // initialize必须放在attachBaseContext最前面，初始化代码直接写在Application类里面，切勿封装到其他类。
        SophixManager.getInstance().setContext(this)
                .setAppVersion(appVersion)
                .setAesKey(null)
                .setSecretMetaData("24832709", "492c7a3fecacfe40824cfcb5688c0e1f", "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC1J5bngsZlBNHPVpJbUBMUTOBdOu1MnIiH+wCPizCKPlPmU84ZIPr5DS/TMUxgnr2TJGrMEqIjun1B20Bu+XLkRNO5bOE6S63r3zpw/qasTv7eHLMeA/kzXLxpFyPmLmIyAdfNX9gAFxBjEs04AhzAjxKclCxcgKRNeqL0bsSkD5c5frUJ2h0G467dxAfsiampsD9IWtVoMNivVvmmJRaRBSkStW1mDsOaUZOaXXzkhcqA9k9CPJ4ro72n6BLYHdViEH3fKrDJOcBCpRUbKNZcDsrirMpWBjSZiwvQqMVzyk5SMsPELH7EtV2KaS04Oa/K6UjifOsj6fr0eCZFL/NvAgMBAAECggEAfmb2a+obRuebAoKUsvRnq7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UovHtetu2GAgE4YJlvv/jqVgQKBgQCyRH1gzdBLv8htl4BvvRDH3iSH+2yZzC8FL+pJukRIUJZdfhtl9f41XU6rBAG7sDMNN1rKel2K91Ho6mwhBYn2QoS5KGFeSIqid+ZPB3RuybSrEDSSqDZxZ8q6vl/AMlvRD2/3M6y4BAmodAVvTotx3aaZcurkYX24QNpRg6jJxwKBgDoOjbz0GQ6Uyt0OwofEYJzICrfav7RR8jEC7yyCCndO53mD1Ap/OVUgY/cRVUIWvmo8Kt5gtkI0TOwM5ZwUGXX3ig02nxR7XJWk8kh5NM0Kj4iBqMU9HbThatD9DiwGFZV7tuxKnoN/pI0HGLUocNsvE7fDCB1sUyXMgpx5hkkBAoGAXLyOcMaAnndVX9WZhgdX7HGelXLxKwzyRQOJIPOaQ/hQULCiticDKGWy3/pEoIvn8u8h9wUzFnDUlpMkzwTfoVE3VE8yX+bi3+P51pDtPBkIOJlEkCqbr+ErsHwKuaQ46YfbWkDykAPhtlUs4pt8pS6GVXtXwJkK2sg6Cn+wVc8CgYBwW1128Qp2JXhXaAIwgWBkrxVHPTAxye6A89g2BsHFQAaIwJIcmbvG29w8bK7qcly7j9pYXsMRjOJrYUtqiTCw+TdyNvPJwQ5vPJQpN8UOM2RC2w5u0x9G3OWum8/Ucs6iIhDFlnN21xPw8pvgU3hlMDp/SqjiWXp0eSGbP77gHw==")
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // 补丁加载回调通知
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {

                            // 表明补丁加载成功
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
                            // 建议: 用户可以监听进入后台事件, 然后调用killProcessSafely自杀，以此加快应用补丁，详见1.3.2.3
                        } else {
                            // 其它错误信息, 查看PatchStatus类说明
                        }
                    }
                }).initialize();

    }

    //static 代码段可以防止内存泄露
    private void refresh() {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.text_delet_main, android.R.color.white);//全局设置主题颜色
                return new WaterDropHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
           /* //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
                @Override
                public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                    //指定为经典Footer，默认是 BallPulseFooter
                    return new ClassicsFooter(context).setDrawableSize(20);
                }
            });*/
    }

    /**
     * 添加Activity
     */
    public void addActivity_(Activity activity) {
        Log.d("rexiufu", "addActivity_");
// 判断当前集合中不存在该Activity
        if (!oList.contains(activity)) {
            oList.add(activity);//把当前Activity添加到集合中
        }
    }

    /**
     * 销毁单个Activity
     */
    public void removeActivity_(Activity activity) {
//判断当前集合中存在该Activity
        if (oList.contains(activity)) {
            oList.remove(activity);//从集合中移除
            activity.finish();//销毁当前Activity
        }
    }

    /**
     * 销毁所有的Activity
     */
    public void removeALLActivity_() {
        //通过循环，把集合中的所有Activity销毁
        for (Activity activity : oList) {
            activity.finish();
        }
    }

    protected void initPrefs() {
        SharedPreferencesUtil.init(context, getPackageName() + "_preference", Context.MODE_MULTI_PROCESS);
    }

    /*public static MyApication getsInstance() {
        if (null == sInstance) {
            sInstance = new MyApication();
        }
        return sInstance;
    }*/
    /*public void exit() {
        try {
            for (Activity activity : oList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
    // 开放Volley的HTTP请求队列接口
   /* public static RequestQueue getRequestQueue() {
        return volleyQueue;
    }*/

}
